
// 尝试手写一个promise
class MyPromise {
  constructor (fn) {
    this.$status = 'pending'
    this.failCallBack = undefined;
    this.successCallback = undefined;
    fn(this.resolve.bind(this), this.reject.bind(this))
  }

  resolve (params) {
    if (this.$status === 'pending') {
      this.$status = 'success'
      this.successCallback(params)
    }
  }

  reject(params) {
    if (this.$status === 'pending') {
      this.$status = 'error'
      this.failCallBack(params)
    }
  }

  then (fn) {
    this.successCallback = fn
    return this
  }

  catch(fn) {
    fn.apply(this, [this.$error])
    return this
  }
}

function getRequest () {
  // 接口请求
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // resolve('请求成功')
      reject('请求失败')
    }, 1000)
  })
}

getRequest().then(res => {
  console.log('123', res)
}).catch(err => {
  console.log('err', err)
})

